﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using f1;
using f1messsagebox;
using f1messabox1;
using System.Xml.Serialization;
namespace WindowsFormf1
{
   
    public partial class Form1 : Form
    {
        BindingList<Order> orders = new BindingList<Order>();
        public Form1()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            using (FileStream fs = new FileStream("Order.xml", FileMode.Open))
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Order>));
                List<Order> list = (List<Order>)xmlSerializer.Deserialize(fs);
                orders=new BindingList<Order>(list);
                dataGridView1.DataSource = orders;
            }
            MessageBox.Show("已成功导入");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (orders.Count == 0)
            {
                MessageBox.Show("无订单");
                return;
            }
            List<Order> list = new List<Order>(orders);
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(BindingList<Order>));
            using (FileStream fs = new FileStream("Order.xml", FileMode.Create))
            {

                xmlSerializer.Serialize(fs, orders);

            }
            MessageBox.Show("已保存到Order.xml中");
        }
        private void button1_Click(object sender, EventArgs e)
        {
            f1messsagebox.Form1 form1 = new f1messsagebox.Form1();
            form1.CreateOrderhandler += new f1messsagebox.ClickHandler(created);
            form1.ShowDialog();
        }
        public void created(object sender,f1messsagebox.CreateEventArgs e)
        {
            e.Order.Id=orders.Count+1;
            orders.Add(e.Order);    
        }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            OrderDetail detail = new OrderDetail(1, "er", 70);
            List<OrderDetail> list = new List<OrderDetail>();
            list.Add(detail);
            //orders.Add(new Order(1,"qwe",100,list));
            //orders.Add(new Order(2, "me", 100, list));
            dataGridView1.DataSource = orders;
            dataGridView2.DataSource = orders;
            dataGridView2.DataMember = "Items";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string str = textBox1.Text;
            int id;
            try
            {
                id = Int32.Parse(str);
            }
            catch (Exception)
            {
                textBox1.Text = "输入正确的订单号";
                return;
            }
            var q = from s in orders
                    where s.Id == id
                    select s;
            BindingList<Order> list = new BindingList<Order>(q.ToList());
            if(list.Count == 0)
            {
                MessageBox.Show("不存在该订单");
            }
            else
            {
                orders.Remove(list[0]);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string str = textBox1.Text;
            int id;
            try
            {
                id=Int32.Parse(str); 
            }
            catch(Exception)
            {
                textBox1.Text = "输入正确的订单号";
                return;
            }
            var q=from s in orders
                  where s.Id==id select s;
            BindingList<Order> list = new BindingList<Order> (q.ToList());
            if( list.Count == 0)
            {
                MessageBox.Show("无订单");
                return;
            }
                
            dataGridView2.DataSource = list;
            dataGridView2.DataMember = "Items";
            dataGridView1.DataSource = list;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource=orders;
            dataGridView2.DataSource = null;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            f1messabox1.Form1 form = new f1messabox1.Form1();
            form.changeOrder += new changeHandler(changed);
            form.ShowDialog();
        }
        public void changed(object sender,f1messabox1.changeEventArgs e)
        {
            Boolean flag = false;
            for(int i = 0; i < orders.Count; i++)
            {
                if(orders[i].Id==e.Id)
                {
                    flag = true;
                    if(e.Str[0]!="")
                    {
                        orders[i].Customer=e.Str[0];
                    }
                    if(e.num[0]!=-1)
                    {
                        orders[i].Amount=e.num[0];
                    }
                    if(e.OrderId!=-1)
                    {
                        for(int j = 0; j < orders[i].Items.Count; j++)
                        {
                            if(orders[i].Items[j].OrderId==e.OrderId)
                            {
                                if(e.num[1]!=-1)
                                {
                                    orders[i].Items[j].Num=e.num[1];
                                }
                                if(e.Str[1]!="")
                                {
                                    orders[i].Items[j].Name=e.Str[1];
                                }
                            }
                        }
                    }
                    
                }
            }
            if(!flag)
            {
                MessageBox.Show("未找到订单");
            }
            dataGridView1.Refresh();
            dataGridView2.Refresh();
        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
 
}
